package com.wl.dzt.oss.impl;

import com.aliyun.oss.internal.OSSConstants;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.wl.dzt.oss.OSSConfig;
import com.wl.dzt.oss.OSSConstant;
import org.apache.commons.io.IOUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/9/009
 * @description 七牛云存储
 */
public class OSSQiniuServiceImpl extends OSSBasicService {


    private UploadManager uploadManager;
    private String token;

    public OSSQiniuServiceImpl(OSSConfig config){
        this.config = config;

        //初始化
        init();
    }

    private void init(){
        uploadManager = new UploadManager(new Configuration(Zone.autoZone()));
//        token = Auth.create(config.getQiniu().get(OSSConstant.QINIU_ACCESS_KEY), config.getQiniu().get(OSSConstant.QINIU_SECRET_KEY)).
//                uploadToken(config.getQiniu().get(OSSConstant.QINIU_BUCKET_NAME));
    }

    @Override
    public String upload(byte[] data, String path) {
//        try {
//            com.qiniu.http.Response res = uploadManager.put(data, path, token);
//            if (!res.isOK()) {
//                throw new RuntimeException("上传七牛出错：" + res.toString());
//            }
//        } catch (Exception e) {
//            throw new RuntimeException("上传文件失败，请核对七牛配置信息", e);
//        }
//
//        return config.getQiniu().get(OSSConstant.QINIU_DOMAIN) + "/" + path;

        return "";
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        try {
            byte[] data = IOUtils.toByteArray(inputStream);
            return this.upload(data, path);
        } catch (IOException e) {
            throw new RuntimeException("上传文件失败", e);
        }
    }

    @Override
    public String uploadSuffix(byte[] data, String suffix) {
//        return upload(data, getPath(config.getQiniu().get(OSSConstant.QINIU_PREFIX), suffix));
        return "";
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix) {
//        return upload(inputStream, getPath(config.getQiniu().get(OSSConstant.QINIU_PREFIX), suffix));
        return "";
    }
}
